
package cfar.parallel.map;

import cfar.ff.fun.F1;
import cfar.ff.fun.F2;
import cfar.ff.fun.Seq;
import cfar.ff.interfaces.Sequence;

import static cfar.ff.fun.Seq.*;

import static fj.control.parallel.Strategy.*;

/**
 *
 * @author badi
 */
public class ParMap {
    public static <S,A,B> F2<F1<A,B>, Sequence<S,A>, Sequence<S,B>> map() {
        return new F2<F1<A, B>, Sequence<S, A>, Sequence<S, B>>() {

            public Sequence<S, B> f(F1<A, B> f, Sequence<S, A> seq) {
                fj.data.List<A> l = Seq.mkList(seq).getList();
                fj.data.List<B> os = (fj.data.List<B>) simpleThreadStrategy().parMap((F1)f, l)._1();

                return mkSeq(os, (Sequence<S,B>) seq.<B>empty());
            }
        };
    }

    public static <S,A,B> Sequence<S,B> map(F1<A,B> f, Sequence<S,A> seq){
        return ParMap.<S,A,B>map().f(f, seq);
    }
}
